<?php
namespace app\api\controller;

use SendGrid\Client;
use SendGrid\Response;

class SendGridService
{
    protected $client;

    /**
     * 构造函数
     * @param string $apiKey SendGrid API Key
     */
    public function __construct($apiKey)
    {
        $this->client = new Client('https://api.sendgrid.com', [
            'Authorization: Bearer ' . $apiKey,
            'Content-Type: application/json'
        ], '/v3');
    }

    /**
     * 发送邮件
     * @param string|array $to       收件人（字符串或 ['email'=>'','name'=>''] 或数组列表）
     * @param string       $subject  标题
     * @param string       $body     邮件正文（纯文本）
     * @param string|array $from     发件人（字符串或 ['email'=>'','name'=>'']）
     * @return Response
     */
    public function sendMail($to, $subject, $body, $from)
    {
        return $this->client->sendMail($to, $subject, $body, $from);
    }

    /**
     * 调用原生 Client 执行其它 API 请求
     * @return Client
     */
    public function client()
    {
        return $this->client;
    }
}
